package test03;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-13
 * Time: 20:14
 * author:Caclu
 */

//实现一个简单的 + 法计算器
public class Test02 {
    public static void main(String[] args) {
        new Caclu().loadFrame();
    }
}

class Caclu extends Frame {
        TextField num1;
        TextField num2;
        TextField num3;

    public void loadFrame() {
        //创建3个输入框
         num1 = new TextField(10);
         num2 = new TextField(10);
         num3 = new TextField(20);
        //创建1个标签
        Label label = new Label("+");
        //创建一个按钮
        Button button1 = new Button("=");
        //为按钮设置监听
        button1.addActionListener( new MyActionListen(this));
        //设置成流式布局
        setLayout(new FlowLayout());
        //添加
        add(num1);
        add(label);
        add(num2);
        add(button1);
        add(num3);
        pack();
        setVisible(true);
    }
}

class MyActionListen implements ActionListener {
    Caclu caclu = null;
    public MyActionListen( Caclu caclu) {
        this.caclu = caclu;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得两个被加数
       int i = Integer.parseInt(caclu.num1.getText());
       int j = Integer.parseInt(caclu.num2.getText());

        //将两个被加数计算后放到第三个框中
        caclu.num3.setText("" + (i + j));

        //清除前两个框的数
        caclu.num1.setText("");
        caclu.num2.setText("");

    }
}